<?php

/**
 * requestshift Function
 *
 * Return available shifts.
 *
 * @author Manas Kalucha <manaskalucha@gmail.com>
 */
include_once 'UTcheckAuth.php';
include_once 'UTerrorcode.php';
include_once 'UTcheckAuth.php';
include_once 'UTDBGlobal.php';

/**
 * @param $userName string
 * @param $userPassHash string
 * @param $desiredShift string
 * @return array
 */
function requestShift($userName, $userPassHash, $desiredShift) {
    global $dbaddress;
    global $dbuser;
    global $dbpassword;
    global $dbdatabasename;

    if (checkAuthentication($userName, $userPassHash) == true) {
        $r = array();
        $conn = new mysqli($dbaddress, $dbuser, $dbpassword, $dbdatabasename);
        $shift = json_decode($desiredShift, true);
        $sql = "SELECT account.empID FROM account WHERE accName = '$userName'";
        $result = $conn->query($sql);
        $row = $result->fetch_assoc();
        $empID = $row['empID'];
        $desiredShift = json_decode($desiredShift, true);
        $sql = "INSERT INTO `proposedchange`(`empID`, `roleID`, `dateID`, `myEmpID`, `myRoleID`, `myDateID`) VALUES (" . $desiredShift['empID'] . "," . $desiredShift['roleID'] . "," . $desiredShift['dateID'] . "," . $empID . "," . $desiredShift['roleID'] . "," . $desiredShift['dateID'] . ")";
        if($conn->query($sql))
        {
            $error = ErrorCode::successCreate;
            $output = array('code' => $error);
        }
        else
        {
            $error = ErrorCode::failCreate;
            $output = array('code' => $error);
        }

        $conn->close();
    } else {
        $error = ErrorCode::authFailRead;
        $output = array('code' => $error);
    }
    return $output;
}

//Test Code
// $a = array('RoleID' => 1 , 'EmpID' =>1, 'DateID' => 1);
// $r = requestShift('user','pass',json_encode($a));
// print_r($r);
?>
